package com.example.userpointmgr.controller;

import com.example.userpointmgr.dao.UserPo;
import com.example.userpointmgr.dao.UserPointChangeDto;
import com.example.userpointmgr.dao.UserRegisterDTO;
import com.example.userpointmgr.service.UserService;
import com.example.userpointmgr.service.WeChatServiceImpl;
import com.example.userpointmgr.tool.PageParam;
import com.example.userpointmgr.tool.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/userinfo")
    @ApiOperation("获取用户信息完成")
    public ResponseResult getUserInfo(@RequestParam String userphonenumber) {
        return userService.getUserInfo(userphonenumber);
    }

    @GetMapping("/userinfoByOpenid")
    @ApiOperation("获取用户信息完成")
    public ResponseResult getUserInfoByOpenid(@RequestParam String openid) {
        return userService.getUserInfoByOpenid(openid);
    }

    @GetMapping("getall")
    @ApiOperation("获取全部用户完成")
    public ResponseResult getUserList(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        PageParam pageParam=new PageParam(pageNum,pageSize);
        return ResponseResult.success(userService.findAllUsers(pageParam));
    }

    @PostMapping("/register")
    @ApiOperation("用户注册完成")
    public ResponseResult userRegister(@RequestBody UserRegisterDTO userRegisterDTO) {
        return userService.userRegister(userRegisterDTO);
    }

    @PostMapping("/deleteuser")
    @ApiOperation("删除接口")
    public ResponseResult deleteUser(@RequestParam String userphonenumber) {
        userService.deleteUser(userphonenumber);
        return ResponseResult.success("删除用户成功");
    }

    @PostMapping("/updateuser")
    @ApiOperation("更新用户，用户管理员修改积分和权限")
    public ResponseResult updateUser(@RequestBody UserPointChangeDto userPointChangeDto) {
        return userService.updateUser(userPointChangeDto);
    }

    //获取openid
    @PostMapping("/code")
    @ApiOperation(("处理微信code"))
    //如果请求的参数是json的就用Map键值对接收
    public ResponseResult getCode(@RequestBody Map<String ,String> request) {
        ObjectMapper mapper = new ObjectMapper();
        //调用微信接口获取sessin_key和openid
        String sessionInfo = WeChatServiceImpl.getSessionKey(request.get("code"));
        try{
            Map<String, String> session = mapper.readValue(sessionInfo, Map.class);
            System.out.println("session_key：" + session.get("session_key"));
            System.out.println("openid:"+session.get("openid"));
            return ResponseResult.success(session);
        }catch (JsonProcessingException e){
            return ResponseResult.fail(e.getMessage());
        }
    }
}
